<script lang="ts" setup>
import type { DressResourceApi } from '#/api/wedding/dressresource';

import { useVbenModal } from '@vben/common-ui';
import { message, Tabs, Checkbox, Input, Textarea, Select,RadioGroup,CheckboxGroup, DatePicker } from 'ant-design-vue';

import { computed, ref } from 'vue';
import { $t } from '#/locales';
import { useVbenForm } from '#/adapter/form';
import { getDressResource, createDressResource, updateDressResource } from '#/api/wedding/dressresource';

import { useFormSchema } from '../data';
import DressImageCropper from './dress-image-cropper.vue';
import ThemeConfigEditor from './theme-config-editor.vue';

const emit = defineEmits(['success']);
const images = ref<string[]>([]);
const themeConfig = ref<DressResourceApi.MobileThemeConfig>({});
const formData = ref<DressResourceApi.DressResource>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['婚纱资源'])
    : $t('ui.actionTitle.create', ['婚纱资源']);
});


const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false
});

const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = (await formApi.getValues()) as DressResourceApi.DressResource;
    // 添加图片数据
    data.images = images.value;
    // 添加主题配置
    data.mobileThemeConfig = themeConfig.value;
    try {
      await (formData.value?.id ? updateDressResource(data) : createDressResource(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success( $t('ui.actionMessage.operationSuccess') );
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      images.value = [];
      themeConfig.value = {};
      return;
    }
    // 加载数据
    let data = modalApi.getData<DressResourceApi.DressResource>();
    if (!data) {
      images.value = [];
      return;
    }
    if (data.id) {
      modalApi.lock();
      try {
        data = await getDressResource(data.id);
      } finally {
        modalApi.unlock();
      }
    }
    // 设置到 values
    formData.value = data;
    images.value = (data.images as any) || [];
    themeConfig.value = data.mobileThemeConfig || {};
    await formApi.setValues(formData.value);
  },
});
</script>

<template>
  <Modal :title="getTitle" class="w-3/5">
    <Tabs default-active-key="basic" class="mx-4">
      <Tabs.TabPane key="basic" tab="基本信息">
        <Form class="mt-4" />
      </Tabs.TabPane>
      
      <Tabs.TabPane key="images" tab="婚纱图片">
        <div class="mt-4">
          <DressImageCropper
            v-model:value="images"
            :max-count="5"
            :max-size="5"
          />
        </div>
      </Tabs.TabPane>
      
      <Tabs.TabPane key="theme" tab="移动端主题">
        <div class="mt-4">
          <ThemeConfigEditor v-model:value="themeConfig" />
        </div>
      </Tabs.TabPane>
    </Tabs>
  </Modal>
</template>
